package util;

import gui.CollectionTabbedPane;
import gui.ItemList;
import gui.ListContextMenu;
import gui.MarketTable;
import gui.ProductGroupSplitPane;
import gui.SystemList;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.SwingUtilities;

import util.Constants.EVEData;
import data.EVEItem;
import data.EVESystem;
import data.ItemSystemCollection;

public class MainMouseListener implements MouseListener {

  private MarketTable     _marketTable;
  private ListContextMenu _contextMenu;

  public MainMouseListener(MarketTable marketTable, ListContextMenu contextMenu) {
    _marketTable = marketTable;
    _contextMenu = contextMenu;
  }

  private void _fillMarketTableWithItemData(ItemSystemCollection collection, EVEItem selectedItem) {
    if (selectedItem != null) {
      if (selectedItem.getItemMarketDataMap().size() > 0) {
        _marketTable.activateSystemListModel(new ItemMarketOrderModel(collection, selectedItem));
      }
    }
    else {
      _marketTable.setModel(new ItemMarketOrderModel(null, null));
      _marketTable.setAutoCreateRowSorter(true);
    }
  }

  private void _fillMarketTableWithSystemData(ItemSystemCollection collection, EVESystem selectedSystem) {
    if (selectedSystem != null) {
      if (selectedSystem.getSystemMarketDataMap().size() > 0) {
        _marketTable.activateSystemListModel(new SystemMarketOrderModel(collection, selectedSystem));
      }
    }
    else {
      _marketTable.setModel(new SystemMarketOrderModel(null, null));
      _marketTable.setAutoCreateRowSorter(true);
    }
  }

  @Override
  public void mouseClicked(MouseEvent event) {
  }

  @Override
  public void mouseEntered(MouseEvent event) {
  }

  @Override
  public void mouseExited(MouseEvent event) {
  }

  @Override
  public void mousePressed(MouseEvent event) {
    if (event.getComponent() instanceof ItemList) {
      ItemList itemList = (ItemList) event.getComponent();
      ProductGroupSplitPane splitPane = (ProductGroupSplitPane) itemList.getParent().getParent().getParent();
      SystemList systemList = splitPane.getSystemList();
      systemList.clearSelection();
      _contextMenu.setVisible(false);

      EVEItem selectedItem = itemList.getSelectedItem();
      _fillMarketTableWithItemData(itemList.getCollection(), selectedItem);
      if (SwingUtilities.isRightMouseButton(event) && selectedItem != null) {
        _contextMenu.showMenu(EVEData.Items, event, splitPane);
      }
    }
    else if (event.getComponent() instanceof SystemList) {
      SystemList systemList = (SystemList) event.getComponent();
      ProductGroupSplitPane splitPane = (ProductGroupSplitPane) systemList.getParent().getParent().getParent();
      ItemList itemList = splitPane.getItemList();
      itemList.clearSelection();
      _contextMenu.setVisible(false);

      EVESystem selectedSystem = systemList.getSelectedSystem();
      _fillMarketTableWithSystemData(systemList.getCollection(), selectedSystem);
      if (SwingUtilities.isRightMouseButton(event) && selectedSystem != null) {
        _contextMenu.showMenu(EVEData.Systems, event, splitPane);
      }
    }
    else if (event.getComponent() instanceof CollectionTabbedPane) {
      ProductGroupSplitPane splitPane = ((CollectionTabbedPane) event.getComponent()).getSelectedPane();
      int itemIndex = splitPane.getItemList().getSelectedIndex();
      int systemIndex = splitPane.getSystemList().getSelectedIndex();
      _contextMenu.setVisible(false);
      if (itemIndex != systemIndex) {
        if (itemIndex > -1) {
          _fillMarketTableWithItemData(splitPane.getItemList().getCollection(), splitPane.getItemList().getSelectedItem());
        }
        else {
          _fillMarketTableWithSystemData(splitPane.getSystemList().getCollection(), splitPane.getSystemList().getSelectedSystem());
        }
      }
      else {
        _fillMarketTableWithItemData(null, null);
      }
    }
  }

  @Override
  public void mouseReleased(MouseEvent event) {
    mousePressed(event);
  }
}
